package svalidator

import (
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
)

type PasswordValidator struct{}

// 1.自定义的校验方法

func (PasswordValidator) validationFunc(fl validator.FieldLevel) bool {
	pwd, ok := fl.Field().Interface().(string)
	if !ok || len(pwd) < 6 {
		return false
	}
	return true
}

// 2.自定义返回信息

func (PasswordValidator) registerFn(ut ut.Translator) error {
	return ut.Add("password_validator", "{0} 长度必须大于6!", true) // see universal-translator for details
}

func (PasswordValidator) translationFn(ut ut.Translator, fe validator.FieldError) string {
	t, _ := ut.T("password_validator", fe.Field())
	return t
}
